package com.inossem.warehousestorage_inossem.utils;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.inossem.warehousestorage_inossem.adapter.OverviewAdapter;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageManageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageMaterielListBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageOperatorBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leij on 2017/8/16.
 */

public class ChangeStorageUtils {

    private static final String IS_FOCUS_TRUE = "1";//允许修改过账日期

    private static final String IS_FOCUS_FALSE = "0"; //不允许修改过账日期

    //获取转储信息
    public static ChangeStorageBean getChangeStorageBean(Context context) {
        String json = SPUtils.getChangeStorageInformation(context);
        if (TextUtils.isEmpty(json)) {
            return new ChangeStorageBean();
        } else {
            Gson gson = new Gson();
            return gson.fromJson(json, ChangeStorageBean.class);
        }
    }

    public static void saveChangeStorageBean(Context context, ChangeStorageBean changeStorageBean) {
        Gson gson = new Gson();
        SPUtils.saveChangeStorageInformation(context, gson.toJson(changeStorageBean, ChangeStorageBean.class));
    }

    public static void updateSpecialInventoryById(Context context, String id, String number, String materielCode, String materielDescription, String factoryCode, String factoryValue, String inventoryAddressCode, String inventoryAddressValue, String warehouseNumber, String specialInventoryCode, String specialInventoryDescription) {
        ChangeStorageBean bean = getChangeStorageBean(context);
        List<ChangeStorageMaterielListBean.DataBean.MaterielListBean> materielList = bean.getMaterielList();
        ChangeStorageMaterielListBean.DataBean.MaterielListBean findMaterielItem = null;
        for (int i = 0; i < materielList.size(); i++) {
            ChangeStorageMaterielListBean.DataBean.MaterielListBean materielItem = materielList.get(i);
            if (materielItem.getId().equals(id)) {
                findMaterielItem = materielItem;
                findMaterielItem.setNumber(number);
                if (!TextUtils.isEmpty(materielCode) && !TextUtils.isEmpty(materielDescription)) {
                    findMaterielItem.setTargetMaterielCode(materielCode);
                    findMaterielItem.setTargetMaterielDescription(materielDescription);
                }
                if (!TextUtils.isEmpty(factoryCode) && !TextUtils.isEmpty(inventoryAddressCode)) {
                    findMaterielItem.setTargetFactoryCode(factoryCode);
                    findMaterielItem.setTargetFactoryValue(factoryValue);
                    findMaterielItem.setTargetInventoryAddressCode(inventoryAddressCode);
                    findMaterielItem.setTargetInventoryAddressValue(inventoryAddressValue);
                    findMaterielItem.setTargetWarehouseNumber(warehouseNumber);
                }
                if (!TextUtils.isEmpty(specialInventoryCode) && !TextUtils.isEmpty(specialInventoryDescription)) {
                    findMaterielItem.setTargetSpecialInventoryCode(specialInventoryCode);
                    findMaterielItem.setTargetSpecialInventoryDescription(specialInventoryDescription);
                }
                materielList.remove(materielItem);
                break;
            }
        }
        if (findMaterielItem != null) {
            materielList.add(0, findMaterielItem);
        }
        bean.setMaterielList(materielList);
        saveChangeStorageBean(context, bean);
    }

    public static ChangeStorageMaterielListBean.DataBean.MaterielListBean getMaterielById(Context context, String id) {
        List<ChangeStorageMaterielListBean.DataBean.MaterielListBean> materielList = getChangeStorageBean(context).getMaterielList();
        for (int i = 0; i < materielList.size(); i++) {
            ChangeStorageMaterielListBean.DataBean.MaterielListBean materielItem = materielList.get(i);
            if (materielItem.getId().equals(id)) {
                return materielItem;
            }
        }
        return null;
    }

    public static List<ChangeStorageManageBean.DataBean.InventoryAddressListBean> getInventoryAddressListByFactoryCode(Context context, String factoryCode) {
        List<ChangeStorageManageBean.DataBean.FactoryListBean> factoryList = getChangeStorageBean(context).getTargetFactoryList();
        for (int i = 0; i < factoryList.size(); i++) {
            ChangeStorageManageBean.DataBean.FactoryListBean factoryItem = factoryList.get(i);
            if (factoryCode.equals(factoryItem.getFactoryCode())) {
                return factoryItem.getInventoryAddressList();
            }
        }
        return new ArrayList<>();
    }

    public static boolean hasMaterielList(Context context) {
        List<ChangeStorageMaterielListBean.DataBean.MaterielListBean> materielList = getChangeStorageBean(context).getMaterielList();
        if (materielList == null || materielList.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    public static void deleteMaterielList(Context context) {
        ChangeStorageBean bean = getChangeStorageBean(context);
        bean.setMaterielList(new ArrayList<>());
        saveChangeStorageBean(context, bean);
    }

    public static void deleteMaterielById(Context context, String id) {
        ChangeStorageBean bean = getChangeStorageBean(context);
        List<ChangeStorageMaterielListBean.DataBean.MaterielListBean> materielList = bean.getMaterielList();
        if (materielList != null && !materielList.isEmpty()) {
            for (int i = 0; i < materielList.size(); i++) {
                if (materielList.get(i).getId().equals(id)) {
                    materielList.remove(i);
                    break;
                }
            }
        }
        bean.setMaterielList(materielList);
        saveChangeStorageBean(context, bean);
    }

    //检查转储管理物料列表是否至少填写一项
    public static int getChangeStorageMaterielNumber(Context context) {
        int result = 0;
        List<ChangeStorageMaterielListBean.DataBean.MaterielListBean> materielList = getChangeStorageBean(context).getMaterielList();
        if (materielList == null || materielList.isEmpty()) {
            return result;
        }
        for (int i = 0; i < materielList.size(); i++) {
            ChangeStorageMaterielListBean.DataBean.MaterielListBean materielItem = materielList.get(i);
            if (!TextUtils.isEmpty(materielItem.getNumber()) && Float.parseFloat(materielItem.getNumber()) > 0) {
                result = result + 1;
            }
        }
        return result;
    }

    //删除转储管理验收人
    public static void deleteOperator(Context context, String id) {
        ChangeStorageBean bean = getChangeStorageBean(context);
        List<ChangeStorageOperatorBean.DataBean.ReceiverListBean> operatorList = bean.getOperatorList();
        for (int i = 0; i < operatorList.size(); i++) {
            ChangeStorageOperatorBean.DataBean.ReceiverListBean operatorItem = operatorList.get(i);
            if (operatorItem.getId().equals(id)) {
                operatorList.remove(operatorItem);
                break;
            }
        }
        bean.setOperatorList(operatorList);
        saveChangeStorageBean(context, bean);
    }

    //是否有转储管理操作人员
    public static boolean hasOperator(Context context) {
        List<ChangeStorageOperatorBean.DataBean.ReceiverListBean> operatorList = getChangeStorageBean(context).getOperatorList();
        if (operatorList != null && !operatorList.isEmpty()) {
            return true;
        }
        return false;
    }

    //添加转储管理验收人
    public static void addOperator(Context context, ChangeStorageOperatorBean.DataBean.ReceiverListBean receiveItem) {
        ChangeStorageBean bean = getChangeStorageBean(context);
        List<ChangeStorageOperatorBean.DataBean.ReceiverListBean> receiverList = bean.getOperatorList() == null ? new ArrayList<>() : bean.getOperatorList();
        receiverList.add(0, receiveItem);
        bean.setOperatorList(receiverList);
        saveChangeStorageBean(context, bean);
    }

    //拼领料出库对象
    public static ChangeStorageConfirmRequestBean getChangeStorageConfirmRequestBean(Context context, String voucherDate, String postDate, boolean isFocus) {
        ChangeStorageConfirmRequestBean result = new ChangeStorageConfirmRequestBean();
        ChangeStorageBean bean = getChangeStorageBean(context);
        result.setFactoryCode(bean.getFactoryCode());
        result.setInventoryAddressCode(bean.getInventoryAddressCode());
        result.setWarehouseNumber(bean.getWarehouseNumber());
        result.setMoveType(bean.getMoveType());
        result.setSpecialInventoryFlag(bean.getSpecialInventory());
        result.setVoucherDate(voucherDate);
        result.setPostDate(postDate);
        result.setBusinessType(bean.getBusinessType());
        result.setDumpNumber(bean.getDumpNumber());
        result.setIsFocus(isFocus ? IS_FOCUS_TRUE : IS_FOCUS_FALSE);
        //操作人
        List<ChangeStorageConfirmRequestBean.OperatorListBean> resultOperatorList = new ArrayList<>();
        List<ChangeStorageOperatorBean.DataBean.ReceiverListBean> operatorList = bean.getOperatorList();
        for (int i = 0; i < operatorList.size(); i++) {
            ChangeStorageConfirmRequestBean.OperatorListBean operator = new ChangeStorageConfirmRequestBean.OperatorListBean();
            operator.setRoleCode(operatorList.get(i).getRoleCode());
            operator.setId(operatorList.get(i).getId());
            resultOperatorList.add(operator);
        }
        result.setOperatorList(resultOperatorList);
        //物料
        List<ChangeStorageConfirmRequestBean.MaterielListBean> resultMaterielList = new ArrayList<>();
        List<ChangeStorageMaterielListBean.DataBean.MaterielListBean> materielList = bean.getMaterielList();
        for (int i = 0; i < materielList.size(); i++) {
            ChangeStorageConfirmRequestBean.MaterielListBean resultMateriel = new ChangeStorageConfirmRequestBean.MaterielListBean();
            ChangeStorageMaterielListBean.DataBean.MaterielListBean materiel = materielList.get(i);
            resultMateriel.setId(materiel.getId());
            resultMateriel.setNumber(materiel.getNumber());
            resultMateriel.setTargetMaterielCode(materiel.getTargetMaterielCode());
            resultMateriel.setTargetFactoryCode(materiel.getTargetFactoryCode());
            resultMateriel.setTargetInventoryAddressCode(materiel.getTargetInventoryAddressCode());
            resultMateriel.setTargetWarehouseNumber(materiel.getTargetWarehouseNumber());
            resultMateriel.setTargetSpecialInventoryCode(materiel.getTargetSpecialInventoryCode());
            resultMateriel.setTargetSpecialInventoryDescription(materiel.getTargetSpecialInventoryDescription());
            if (!TextUtils.isEmpty(resultMateriel.getNumber())) {
                resultMaterielList.add(resultMateriel);
            }
        }
        result.setMaterielList(resultMaterielList);
        return result;
    }

    public static void updateDumpNumber(Context context, String dumpNumber) {
        ChangeStorageBean bean = getChangeStorageBean(context);
        bean.setDumpNumber(dumpNumber);
        saveChangeStorageBean(context, bean);
    }

    //获取转储行项目概览
    public static List<OverviewAdapter.OverviewBean> getOverViewBeanWithChangeStorageList(List<ChangeStorageMaterielListBean.DataBean.MaterielListBean> materielList) {
        List<OverviewAdapter.OverviewBean> resultList = new ArrayList<>();
        for (int i = 0; i < materielList.size(); i++) {
            ChangeStorageMaterielListBean.DataBean.MaterielListBean materielItem = materielList.get(i);
            if (!TextUtils.isEmpty(materielItem.getNumber())) {
                OverviewAdapter.OverviewBean overItem = new OverviewAdapter.OverviewBean();
                overItem.setInfo(materielItem.getMaterielDescription());
                overItem.setNumber(materielItem.getNumber() + materielItem.getUnit());
                resultList.add(overItem);
            }
        }
        return resultList;
    }

}
